package com.robusta.cityuate.fragments;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.robusta.cityuate.R;
import com.robusta.cityuate.adapters.CommentsAdapter;
import com.robusta.cityuate.objects.Comment;
import com.robusta.cityuate.objects.Spot;
import com.robusta.cityuate.services.CityuateBroadCastReceiver;
import com.robusta.cityuate.utils.ConnectionUtils;
import com.robusta.cityuate.utils.Token;
import com.robusta.cityuate.utils.UIUtils;

public class SpotCommentsFragmemt extends DialogFragment {

	private ListView commentsList;
	private Activity mContext;
	CommentsAdapter commentsAdapter;
	Spot spot;
	GetComments getCommentsAsyncTask;
	ProgressBar progressBar;
	private EditText commentBodyField;
	private Button commentButton;
	Comment comment;
	public String COMMENT_TAG = SpotCommentsFragmemt.class.getName()
			+ "_COMMENTSPOT";


	private CityuateBroadCastReceiver commentReceiver = new CityuateBroadCastReceiver() {

		@Override
		public void onSuccess() {
			commentButton.setEnabled(true);
			commentBodyField.setEnabled(true);
			commentBodyField.setText(null);
			commentsAdapter.addItem(comment);
			commentsAdapter.notifyDataSetChanged();
			ConnectionUtils.clearConnection(mContext, COMMENT_TAG);
		}

		@Override
		public void onFailure() {
			commentButton.setEnabled(true);
			commentBodyField.setEnabled(true);
			Toast.makeText(mContext,
					ConnectionUtils.getExtra(mContext, COMMENT_TAG),
					Toast.LENGTH_SHORT).show();
			ConnectionUtils.clearConnection(mContext, COMMENT_TAG);
		}

		@Override
		public void onCancel() {
			commentButton.setEnabled(true);
			commentBodyField.setEnabled(true);
			commentBodyField.setText(null);
			ConnectionUtils.clearConnection(mContext, COMMENT_TAG);
		}
	};

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.spot_comments_list_fragment, container,true);
		commentsList = (ListView) v.findViewById(R.id.comments_list);
		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		progressBar=(ProgressBar) v.findViewById(R.id.loading);
		commentBodyField=(EditText) v.findViewById(R.id.comment_field);
		commentButton=(Button) v.findViewById(R.id.btn_comment);
		commentButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				commentButton.setEnabled(false);
				commentBodyField.setEnabled(false);
				sendComment();
			}
		});
		return v;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getCommentsAsyncTask=new GetComments();
		
	}
	
	public static SpotCommentsFragmemt getInstance(Spot spot) {
		SpotCommentsFragmemt cf=new SpotCommentsFragmemt();
		
		cf.spot=spot;
		cf.COMMENT_TAG+=cf.spot.getId();
		return cf;

	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		TextView emptyList=new TextView(mContext);
		emptyList.setText(R.string.no_comments);
		commentsList.setEmptyView(emptyList);
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mContext.registerReceiver(commentReceiver, new IntentFilter(COMMENT_TAG));
		if (ConnectionUtils.wasConnected(mContext, COMMENT_TAG))
			// connection was successful
			if (ConnectionUtils.isResultSuccessful(mContext, COMMENT_TAG))
				commentReceiver.onSuccess();
			else
				commentReceiver.onFailure();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = activity;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		 commentsAdapter = new CommentsAdapter(mContext,
				UIUtils.getScreenWidth(getActivity()));
		commentsList.setAdapter(commentsAdapter);
		// load comments
		getCommentsAsyncTask.execute("");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		mContext.unregisterReceiver(commentReceiver);
	}
	
	@Override
	public void onDestroy() {
		// cancel getting comments on destroy
		if (!ConnectionUtils.isConnecting(mContext, COMMENT_TAG)) {
			ConnectionUtils.clearConnection(mContext, COMMENT_TAG);
		}
		
		getCommentsAsyncTask.cancel(true);
		super.onDestroy();
	}
	
	private class GetComments extends AsyncTask<String, Void, String>{

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected String doInBackground(String... params) {
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
			}
			return "";
		}
		
		@Override
		protected void onPostExecute(String result) {
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text"));
			commentsAdapter.addItem(new Comment("I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text I am here .. new comment full of text"));
			commentsAdapter.notifyDataSetChanged();
			progressBar.setVisibility(View.GONE);
			super.onPostExecute(result);
		}
		
	}
	
	public String getCommentBody(){
		return commentBodyField.getText().toString();
	}
	
	public void sendComment(){
		comment= new Comment(Token.getUser(mContext),getCommentBody(),spot);
		comment.send(mContext, COMMENT_TAG);
	}
}
